import { BaseWindow } from "../../../../component/window/BaseWindow";
import { ILayer } from "../../../../mvc/interfaces/ILayer";
import { ObjCreate } from "../../common/ObjCreate";
import { ImagesConst } from "../../../resource/ImagesConst";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { HTMLUtil } from "../../../../../com/gengine/utils/HTMLUtil";
import { Language } from "../../../../../extend/language/Language";
import { CrossCopyPlayerStateItem } from "./CrossCopyPlayerStateItem";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { SCrossCopyCostNotice } from "../../../../../Message/Public/SCrossCopyCostNotice";
import { SEntityId } from "../../../../../Message/Public/SEntityId";
import { CopyConfig } from "../../../resource/CopyConfig";
import { GameDefConfig } from "../../../resource/GameDefConfig";
import { EPrictUnit } from "../../../../../Message/Public/EPrictUnit";
import { Cache } from "../../../cache/Cache";
import { ECopyType } from "../../../../../Message/Public/ECopyType";
import { EDifficultyOption } from "../../../../../Message/Public/EDifficultyOption";
import { CopyUtil } from "../CopyUtil";
import { CrossCopyPlayerStateData } from "./CrossCopyPlayerStateData";
import { EntityUtil } from "../../../scene/layer/utils/EntityUtil";
import { SCrossCopyCheckState } from "../../../../../Message/Public/SCrossCopyCheckState";
import { SecTimer } from "../../../../../com/gengine/core/frame/SecTimer";
import { TimerType } from "../../../../../com/gengine/core/frame/TimerType";
import { TCopy } from "../../../../../Message/Db/Tables/TCopy";
import { GTextFiled } from "../../../../../com/mui/controls/GTextFiled";
import { GTileList } from "../../../../../com/mui/controls/GTileList";
import { GButton } from "../../../../../com/mui/controls/GButton";
type int = number;
//class CrossCopyGroupEnterPanel
    
    export  class CrossCopyGroupEnterPanel extends BaseWindow
    {
        constructor(arg1: ILayer=null)
        {
            super(arg1);
            this.setSize(380, 330);
            this.titleHeight = 25;
            return;
        }

        protected /* override */ createChildren(): void
        {
            super.createChildren();
            ObjCreate.createBg(18, 38, 340, 280, this);
            ObjCreate.createBg(30, 87, 320, 2, this, ImagesConst.SplitLine);
            ObjCreate.createBg(30, 275, 320, 2, this, ImagesConst.SplitLine);
            this._text = ObjCreate.createGTextField("", 30, 48, 320, 40, this, GlobalStyle.setTextFormatLeading(GlobalStyle.textFormat1, 3));
            this._text.wordWrap = true;
            this._text.multiline = true;
            var loc1=ObjCreate.createGTextField("", 30, 92, 320, 20, this);
            loc1.htmlText = HTMLUtil.addColor(Language.getString(60906), "#FFFF00");
            this._maxProcess = ObjCreate.createGTextField("", 265, 92, 100, 20, this);
            this._maxProcess.htmlText = HTMLUtil.addColor(Language.getString(60463), "#FFFF00");
            this._tileList = ObjCreate.createTileList(30, 117, 330, 160, this);
            this._tileList.columnWidth = 330;
            this._tileList.rowHeight = 30;
            this._tileList.setStyle("cellRenderer", CrossCopyPlayerStateItem);
            this._dp = new DataProvider();
            this._tileList.dataProvider = this._dp;
            this._okBtn = ObjCreate.createGButton(Language.getString(69901), 90, 285, 75, 26, this);
            this._okBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onOkButtonClick);
            this._cancelBtn = ObjCreate.createGButton(Language.getString(69902), 215, 285, 75, 26, this);
            this._cancelBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onCancelButtonClick);
            return;
        }

        /* internal  */onOkButtonClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.CopyGroupCrossCopyConfirm, this._copyCode));
            return;
        }

        /* internal  */onCancelButtonClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.CopyGroupCrossCopyCancel, this._copyCode));
            return;
        }

        public updateCostNotice(arg1: SCrossCopyCostNotice, arg2: Array<any>, arg3: SEntityId, arg4: Boolean): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=null;
            this._copyCode = arg1.copyCode;
            this._copy = CopyConfig.instance.getInfoByCode(this._copyCode);
            if (this._copy) 
            {
                this.title = ObjCreate.createTitleTextField(this._copy.name);
            }
            this._maxProcess.visible = arg4;
            if (arg1.costCoin > 0) 
            {
                loc2 = arg1.costCoin;
                loc3 = GameDefConfig.instance.getEPrictUnitName(EPrictUnit._EPriceUnitCoin);
            }
            else if (arg1.costGold > 0) 
            {
                loc2 = arg1.costGold;
                loc3 = GameDefConfig.instance.getEPrictUnitName(EPrictUnit._EPriceUnitGold);
            }
            loc1 = Cache.instance.copy.getCopyNumInfoByCode(this._copyCode);
            if (this._copy && !(this._copy.copyType == ECopyType._ECopyCrossTower) && this._copy.difficultyNum == EDifficultyOption._EDifficultyHard && loc1 && loc1.todayEnterNum >= CopyUtil.getCopyNumByDay(this._copy)) 
            {
                this._text.htmlText = GameDefConfig.instance.getHeroCopyExplain(this._copyCode);
            }
            else if (this._copy.copyType != ECopyType._ECopyCrossTower) 
            {
                this._text.htmlText = Language.getStringByParam(60901, loc2, loc3);
            }
            else if (loc1.todayEnterNum > 0) 
            {
                this._text.htmlText = "";
            }
            else 
            {
                this._text.htmlText = Language.getStringByParam(60901, loc2, loc3);
            }
            this.updateSecTimer(arg1.leftTime);
            this.initGroupPlayerStates(arg2, arg3);
            this.updateCanOpt(true);
            return;
        }

        /* internal  */initGroupPlayerStates(arg1: Array<any>, arg2: SEntityId): void
        {
            var loc1=null;
            var loc2=null;
            this._dp.removeAll();
            var loc3=0;
            var loc4=arg1;
            for(loc1 of loc4) 
            {
                (loc2 = new CrossCopyPlayerStateData()).player = loc1;
                loc2.isCaptain = EntityUtil.equal(loc1.entityId, arg2);
                loc2.ready = false;
                loc2.copyId = this._copy.copyId;
                this._dp.addItem(loc2);
            }
            this._tileList.dataProvider = this._dp;
            return;
        }

        public updateGroupPlayersState(arg1: SCrossCopyCheckState, arg2: SEntityId): void
        {
            var loc1=null;
            var loc2=true;
            var loc3=0;
            while (loc3 < this._dp.length) 
            {
                loc1 = this._dp.getItemAt(loc3) as CrossCopyPlayerStateData;
                if (EntityUtil.equal(loc1.player.entityId, arg1.entityId)) 
                {
                    loc1.responds = true;
                    loc1.ready = arg1.isSuccess;
                }
                if (loc1.responds == false) 
                {
                    loc2 = false;
                }
                ++loc3;
            }
            this._tileList.dataProvider = this._dp;
            if (EntityUtil.equal(arg1.entityId, arg2)) 
            {
                this.updateCanOpt(false);
            }
            if (loc2) 
            {
                flash.utils.setTimeout(this.onAllResTimeOut, 1000);
            }
            return;
        }

        /* internal  */onAllResTimeOut(): void
        {
            if (!isHide) 
            {
                this.hide();
            }
            return;
        }

        /* internal  */updateCanOpt(arg1: Boolean): void
        {
            this._canOpt = arg1;
            this._okBtn.enabled = this._canOpt;
            this._cancelBtn.enabled = this._canOpt;
            return;
        }

        /* internal  */updateSecTimer(arg1: int): void
        {
            if (!this._secTimer) 
            {
                this._secTimer = new SecTimer();
            }
            this._secTimer.reset();
            this._secTimer.repeatCount = arg1;
            this._secTimer.addListener(TimerType.ENTERFRAME, this.onSecTimerEnterFrame);
            this._secTimer.addListener(TimerType.COMPLETE, this.onSecTimerComplete);
            this._secTimer.start();
            return;
        }

        /* internal  */onSecTimerEnterFrame(arg1: SecTimer): void
        {
            this._cancelBtn.label = Language.getString(69902) + "（" + arg1.repeatCount + "）";
            return;
        }

        /* internal  */onSecTimerComplete(arg1: SecTimer): void
        {
            this.disposeTimer();
            this.hide();
            return;
        }

        /* internal  */disposeTimer(): void
        {
            if (this._secTimer) 
            {
                this._secTimer.dispose();
                this._secTimer = null;
            }
            return;
        }

        protected /* override */ closeBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._canOpt) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.CopyGroupCrossCopyCancel));
            }
            super.closeBtnClickHandler(arg1);
            return;
        }

        public /* override */ hide(): void
        {
            super.hide();
            this.disposeTimer();
            return;
        }

        /* internal */ /* var */_copyCode: int;

        /* internal */ /* var */_copy: TCopy;

        /* internal */ /* var */_secTimer: SecTimer;

        /* internal */ /* var */_text: GTextFiled;

        /* internal */ /* var */_maxProcess: GTextFiled;

        /* internal */ /* var */_tileList: GTileList;

        /* internal */ /* var */_dp: DataProvider;

        /* internal */ /* var */_okBtn: GButton;

        /* internal */ /* var */_cancelBtn: GButton;

        /* internal */ /* var */_canOpt: Boolean;
    }
